package com.znjc.web.controller.device;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.znjc.common.annotation.Log;
import com.znjc.common.core.controller.BaseController;
import com.znjc.common.core.domain.AjaxResult;
import com.znjc.common.core.domain.entity.SysUser;
import com.znjc.common.enums.BusinessType;
import com.znjc.device.domain.IotDataCheck;
import com.znjc.device.domain.IotDataCheckRuleRef;
import com.znjc.device.domain.IotDataResRule;
import com.znjc.device.service.IIotDataCheckRuleRefService;
import com.znjc.device.service.IIotDataCheckService;
import com.znjc.device.service.IIotDataResRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【校验与规则】Controller
 *
 * @author zengpc
 * @date 2023-12-04
 */
@RestController
@RequestMapping("/device/checkRuleRef")
public class IotDataCheckRuleRefController extends BaseController {
    @Autowired
    private IIotDataCheckRuleRefService iotDataCheckRuleRefService;
    @Autowired
    private IIotDataCheckService iotDataCheckService;
    @Autowired
    private IIotDataResRuleService iotDataResRuleService;

    /**
     * 根据串口id获取授权设备类型
     */
    @PreAuthorize("@ss.hasPermi('device:checkRuleRef:edit')")
    @GetMapping("/authIotDataResRule/{dataCheckId}")
    public AjaxResult authIotDataResRule(@PathVariable("dataCheckId") Long dataCheckId) {
        AjaxResult ajax = AjaxResult.success();
        IotDataCheck iotDataCheck = iotDataCheckService.getById(dataCheckId);
        ajax.put("iotDataCheck", iotDataCheck);
        List<IotDataResRule> iotDataResRules = iotDataResRuleService.list();
        if (CollectionUtils.isNotEmpty(iotDataResRules)) {
            List<Long> iotDataResRuleIds = iotDataCheckRuleRefService.getIdList(dataCheckId, 1);
            if (CollectionUtils.isNotEmpty(iotDataResRuleIds)) {
                iotDataResRules.stream().map(d -> {
                    if (iotDataResRuleIds.contains(d.getId())) {
                        d.setFlag(true);
                    }
                    return d;
                }).collect(Collectors.toList());
            }
            ajax.put("iotDataResRules", SysUser.isAdmin(dataCheckId) ? iotDataResRules : iotDataResRules.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        }
        return ajax;
    }

    /**
     * 串口绑定设备类型
     */
    @PreAuthorize("@ss.hasPermi('device:checkRuleRef:edit')")
    @Log(title = "设备类型绑定串口管理", businessType = BusinessType.GRANT)
    @PutMapping("/authIotDataResRule")
    public AjaxResult insertAuthRole(Long dataCheckId, Long[] iotDataResRuleIds) {
        if (dataCheckId != null && iotDataResRuleIds.length == 0) {
            LambdaQueryWrapper<IotDataCheckRuleRef> wrapper = new LambdaQueryWrapper<IotDataCheckRuleRef>();
            wrapper.eq(IotDataCheckRuleRef::getDataCheckId, dataCheckId);
            iotDataCheckRuleRefService.remove(wrapper);
            return success();
        }
        List<IotDataCheckRuleRef> refList = new ArrayList<IotDataCheckRuleRef>();
        if (dataCheckId != null && iotDataResRuleIds.length > 0) {
            IotDataCheckRuleRef ref = null;
            for (int i = 0; i < iotDataResRuleIds.length; i++) {
                ref = new IotDataCheckRuleRef();
                ref.setResRuleId(iotDataResRuleIds[i]);
                ref.setDataCheckId(dataCheckId);
                refList.add(ref);
            }
        }
        if (CollectionUtils.isNotEmpty(refList)) {
            //先删除
            LambdaQueryWrapper<IotDataCheckRuleRef> wrapper = new LambdaQueryWrapper<IotDataCheckRuleRef>();
            wrapper.eq(IotDataCheckRuleRef::getDataCheckId, dataCheckId);
            iotDataCheckRuleRefService.remove(wrapper);
            //再保存
            iotDataCheckRuleRefService.saveBatch(refList);
        }
        return success();
    }

}
